home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / util / Arrays.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  75.0 KB  |  2,189 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)Arrays.java    1.26 98/09/30
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.util;
  16.  
  17. /**
  18.  * This class contains various methods for manipulating arrays (such as
  19.  * sorting and searching).  It also contains a static factory that allows
  20.  * arrays to be viewed as lists.<p>
  21.  *
  22.  * The documentation for the sorting and searching methods contained in this
  23.  * class includes briefs description of the <i>implementations</i>.  Such
  24.  * descriptions should be regarded as <i>implementation notes</i>, rather than
  25.  * parts of the <i>specification</i>.  Implementors should feel free to
  26.  * substitute other algorithms, so long as the specification itself is adhered
  27.  * to.  (For example, the algorithm used by <tt>sort(Object[])</tt> does not
  28.  * have to be a mergesort, but it does have to be <i>stable</i>.)
  29.  *
  30.  * @author  Josh Bloch
  31.  * @version 1.26 09/30/98
  32.  * @see Comparable
  33.  * @see Comparator
  34.  * @since JDK1.2
  35.  */
  36.  
  37. public class Arrays {
  38.     // Suppresses default constructor, ensuring non-instantiability.
  39.     private Arrays() {
  40.     }
  41.  
  42.     // Sorting
  43.  
  44.     /**
  45.      * Sorts the specified array of longs into ascending numerical order.
  46.      * The sorting algorithm is a tuned quicksort, adapted from Jon
  47.      * L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function",
  48.      * Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November
  49.      * 1993).  This algorithm offers n*log(n) performance on many data sets
  50.      * that cause other quicksorts to degrade to quadratic performance.
  51.      *
  52.      * @param a the array to be sorted.
  53.      */
  54.     public static void sort(long[] a) {
  55.     sort1(a, 0, a.length);
  56.     }
  57.  
  58.     /**
  59.      * Sorts the specified range of the specified array of longs into
  60.      * ascending numerical order.  The sorting algorithm is a tuned quicksort,
  61.      * adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a
  62.      * Sort Function", Software-Practice and Experience, Vol. 23(11)
  63.      * P. 1249-1265 (November 1993).  This algorithm offers n*log(n)
  64.      * performance on many data sets that cause other quicksorts to degrade to
  65.      * quadratic performance.
  66.      *
  67.      * @param a the array to be sorted.
  68.      * @param fromIndex the index of the first element (inclusive) to be
  69.      *        sorted.
  70.      * @param toIndex the index of the last element (exclusive) to be sorted.
  71.      * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt>
  72.      * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or
  73.      *           <tt>toIndex > a.length</tt>
  74.      */
  75.     public static void sort(long[] a, int fromIndex, int toIndex) {
  76.         rangeCheck(a.length, fromIndex, toIndex);
  77.     sort1(a, fromIndex, toIndex-fromIndex);
  78.     }
  79.  
  80.     /**
  81.      * Sorts the specified array of ints into ascending numerical order.
  82.      * The sorting algorithm is a tuned quicksort, adapted from Jon
  83.      * L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function",
  84.      * Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November
  85.      * 1993).  This algorithm offers n*log(n) performance on many data sets
  86.      * that cause other quicksorts to degrade to quadratic performance.
  87.      *
  88.      * @param a the array to be sorted.
  89.      */
  90.     public static void sort(int[] a) {
  91.     sort1(a, 0, a.length);
  92.     }
  93.  
  94.     /**
  95.      * Sorts the specified range of the specified array of ints into
  96.      * ascending numerical order.  The sorting algorithm is a tuned quicksort,
  97.      * adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a
  98.      * Sort Function", Software-Practice and Experience, Vol. 23(11)
  99.      * P. 1249-1265 (November 1993).  This algorithm offers n*log(n)
  100.      * performance on many data sets that cause other quicksorts to degrade to
  101.      * quadratic performance.
  102.      *
  103.      * @param a the array to be sorted.
  104.      * @param fromIndex the index of the first element (inclusive) to be
  105.      *        sorted.
  106.      * @param toIndex the index of the last element (exclusive) to be sorted.
  107.      * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt>
  108.      * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or
  109.      *           <tt>toIndex > a.length</tt>
  110.      */
  111.     public static void sort(int[] a, int fromIndex, int toIndex) {
  112.         rangeCheck(a.length, fromIndex, toIndex);
  113.     sort1(a, fromIndex, toIndex-fromIndex);
  114.     }
  115.  
  116.     /**
  117.      * Sorts the specified array of shorts into ascending numerical order.
  118.      * The sorting algorithm is a tuned quicksort, adapted from Jon
  119.      * L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function",
  120.      * Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November
  121.      * 1993).  This algorithm offers n*log(n) performance on many data sets
  122.      * that cause other quicksorts to degrade to quadratic performance.
  123.      *
  124.      * @param a the array to be sorted.
  125.      */
  126.     public static void sort(short[] a) {
  127.     sort1(a, 0, a.length);
  128.     }
  129.  
  130.     /**
  131.      * Sorts the specified range of the specified array of shorts into
  132.      * ascending numerical order.  The sorting algorithm is a tuned quicksort,
  133.      * adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a
  134.      * Sort Function", Software-Practice and Experience, Vol. 23(11)
  135.      * P. 1249-1265 (November 1993).  This algorithm offers n*log(n)
  136.      * performance on many data sets that cause other quicksorts to degrade to
  137.      * quadratic performance.
  138.      *
  139.      * @param a the array to be sorted.
  140.      * @param fromIndex the index of the first element (inclusive) to be
  141.      *        sorted.
  142.      * @param toIndex the index of the last element (exclusive) to be sorted.
  143.      * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt>
  144.      * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or
  145.      *           <tt>toIndex > a.length</tt>
  146.      */
  147.     public static void sort(short[] a, int fromIndex, int toIndex) {
  148.         rangeCheck(a.length, fromIndex, toIndex);
  149.     sort1(a, fromIndex, toIndex-fromIndex);
  150.     }
  151.  
  152.     /**
  153.      * Sorts the specified array of chars into ascending numerical order.
  154.      * The sorting algorithm is a tuned quicksort, adapted from Jon
  155.      * L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function",
  156.      * Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November
  157.      * 1993).  This algorithm offers n*log(n) performance on many data sets
  158.      * that cause other quicksorts to degrade to quadratic performance.
  159.      *
  160.      * @param a the array to be sorted.
  161.      */
  162.     public static void sort(char[] a) {
  163.     sort1(a, 0, a.length);
  164.     }
  165.  
  166.     /**
  167.      * Sorts the specified range of the specified array of chars into
  168.      * ascending numerical order.  The sorting algorithm is a tuned quicksort,
  169.      * adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a
  170.      * Sort Function", Software-Practice and Experience, Vol. 23(11)
  171.      * P. 1249-1265 (November 1993).  This algorithm offers n*log(n)
  172.      * performance on many data sets that cause other quicksorts to degrade to
  173.      * quadratic performance.
  174.      *
  175.      * @param a the array to be sorted.
  176.      * @param fromIndex the index of the first element (inclusive) to be
  177.      *        sorted.
  178.      * @param toIndex the index of the last element (exclusive) to be sorted.
  179.      * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt>
  180.      * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or
  181.      *           <tt>toIndex > a.length</tt>
  182.      */
  183.     public static void sort(char[] a, int fromIndex, int toIndex) {
  184.         rangeCheck(a.length, fromIndex, toIndex);
  185.     sort1(a, fromIndex, toIndex-fromIndex);
  186.     }
  187.  
  188.     /**
  189.      * Sorts the specified array of bytes into ascending numerical order.
  190.      * The sorting algorithm is a tuned quicksort, adapted from Jon
  191.      * L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function",
  192.      * Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November
  193.      * 1993).  This algorithm offers n*log(n) performance on many data sets
  194.      * that cause other quicksorts to degrade to quadratic performance.
  195.      *
  196.      * @param a the array to be sorted.
  197.      */
  198.     public static void sort(byte[] a) {
  199.     sort1(a, 0, a.length);
  200.     }
  201.  
  202.     /**
  203.      * Sorts the specified range of the specified array of bytes into
  204.      * ascending numerical order.  The sorting algorithm is a tuned quicksort,
  205.      * adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a
  206.      * Sort Function", Software-Practice and Experience, Vol. 23(11)
  207.      * P. 1249-1265 (November 1993).  This algorithm offers n*log(n)
  208.      * performance on many data sets that cause other quicksorts to degrade to
  209.      * quadratic performance.
  210.      *
  211.      * @param a the array to be sorted.
  212.      * @param fromIndex the index of the first element (inclusive) to be
  213.      *        sorted.
  214.      * @param toIndex the index of the last element (exclusive) to be sorted.
  215.      * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt>
  216.      * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or
  217.      *           <tt>toIndex > a.length</tt>
  218.      */
  219.     public static void sort(byte[] a, int fromIndex, int toIndex) {
  220.         rangeCheck(a.length, fromIndex, toIndex);
  221.     sort1(a, fromIndex, toIndex-fromIndex);
  222.     }
  223.  
  224.     /**
  225.      * Sorts the specified array of doubles into ascending numerical order.
  226.      * The sorting algorithm is a tuned quicksort, adapted from Jon
  227.      * L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function",
  228.      * Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November
  229.      * 1993).  This algorithm offers n*log(n) performance on many data sets
  230.      * that cause other quicksorts to degrade to quadratic performance.
  231.      *
  232.      * @param a the array to be sorted.
  233.      */
  234.     public static void sort(double[] a) {
  235.     sort2(a, 0, a.length);
  236.     }
  237.  
  238.     /**
  239.      * Sorts the specified range of the specified array of doubles into
  240.      * ascending numerical order.  The sorting algorithm is a tuned quicksort,
  241.      * adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a
  242.      * Sort Function", Software-Practice and Experience, Vol. 23(11)
  243.      * P. 1249-1265 (November 1993).  This algorithm offers n*log(n)
  244.      * performance on many data sets that cause other quicksorts to degrade to
  245.      * quadratic performance.
  246.      *
  247.      * @param a the array to be sorted.
  248.      * @param fromIndex the index of the first element (inclusive) to be
  249.      *        sorted.
  250.      * @param toIndex the index of the last element (exclusive) to be sorted.
  251.      * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt>
  252.      * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or
  253.      *           <tt>toIndex > a.length</tt>
  254.      */
  255.     public static void sort(double[] a, int fromIndex, int toIndex) {
  256.         rangeCheck(a.length, fromIndex, toIndex);
  257.     sort2(a, fromIndex, toIndex);
  258.     }
  259.  
  260.     /**
  261.      * Sorts the specified array of floats into ascending numerical order.
  262.      * The sorting algorithm is a tuned quicksort, adapted from Jon
  263.      * L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function",
  264.      * Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November
  265.      * 1993).  This algorithm offers n*log(n) performance on many data sets
  266.      * that cause other quicksorts to degrade to quadratic performance.
  267.      *
  268.      * @param a the array to be sorted.
  269.      */
  270.     public static void sort(float[] a) {
  271.     sort2(a, 0, a.length);
  272.     }
  273.  
  274.     /**
  275.      * Sorts the specified range of the specified array of floats into
  276.      * ascending numerical order.  The sorting algorithm is a tuned quicksort,
  277.      * adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a
  278.      * Sort Function", Software-Practice and Experience, Vol. 23(11)
  279.      * P. 1249-1265 (November 1993).  This algorithm offers n*log(n)
  280.      * performance on many data sets that cause other quicksorts to degrade to
  281.      * quadratic performance.
  282.      *
  283.      * @param a the array to be sorted.
  284.      * @param fromIndex the index of the first element (inclusive) to be
  285.      *        sorted.
  286.      * @param toIndex the index of the last element (exclusive) to be sorted.
  287.      * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt>
  288.      * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or
  289.      *           <tt>toIndex > a.length</tt>
  290.      */
  291.     public static void sort(float[] a, int fromIndex, int toIndex) {
  292.         rangeCheck(a.length, fromIndex, toIndex);
  293.     sort2(a, fromIndex, toIndex);
  294.     }
  295.  
  296.     private static void sort2(double a[], int fromIndex, int toIndex) {
  297.         final long NEG_ZERO_BITS = Double.doubleToLongBits(-0.0d);
  298.         /*
  299.          * The sort is done in three phases to avoid the expense of using
  300.          * NaN and -0.0 aware comparisons during the main sort.
  301.          */
  302.  
  303.         /*
  304.          * Preprocessing phase:  Move any NaN's to end of array, count the
  305.          * number of -0.0's, and turn them into 0.0's. 
  306.          */
  307.         int numNegZeros = 0;
  308.         int i = fromIndex, n = toIndex;
  309.         while(i < n) {
  310.             if (a[i] != a[i]) {
  311.                 a[i] = a[--n];
  312.                 a[n] = Double.NaN;
  313.             } else {
  314.                 if (a[i]==0 && Double.doubleToLongBits(a[i])==NEG_ZERO_BITS) {
  315.                     a[i] = 0.0d;
  316.                     numNegZeros++;
  317.                 }
  318.                 i++;
  319.             }
  320.         }
  321.  
  322.         // Main sort phase: quicksort everything but the NaN's
  323.     sort1(a, fromIndex, n-fromIndex);
  324.  
  325.         // Postprocessing phase: change 0.0's to -0.0's as required
  326.         if (numNegZeros != 0) {
  327.             int j = binarySearch(a, 0.0d, fromIndex, n-1); // posn of ANY zero
  328.             do {
  329.                 j--;
  330.             } while (j>=0 && a[j]==0.0d);
  331.  
  332.             // j is now one less than the index of the FIRST zero
  333.             for (int k=0; k<numNegZeros; k++)
  334.                 a[++j] = -0.0d;
  335.         }
  336.     }
  337.  
  338.  
  339.     private static void sort2(float a[], int fromIndex, int toIndex) {
  340.         final int NEG_ZERO_BITS = Float.floatToIntBits(-0.0f);
  341.         /*
  342.          * The sort is done in three phases to avoid the expense of using
  343.          * NaN and -0.0 aware comparisons during the main sort.
  344.          */
  345.  
  346.         /*
  347.          * Preprocessing phase:  Move any NaN's to end of array, count the
  348.          * number of -0.0's, and turn them into 0.0's. 
  349.          */
  350.         int numNegZeros = 0;
  351.         int i = fromIndex, n = toIndex;
  352.         while(i < n) {
  353.             if (a[i] != a[i]) {
  354.                 a[i] = a[--n];
  355.                 a[n] = Float.NaN;
  356.             } else {
  357.                 if (a[i]==0 && Float.floatToIntBits(a[i])==NEG_ZERO_BITS) {
  358.                     a[i] = 0.0f;
  359.                     numNegZeros++;
  360.                 }
  361.                 i++;
  362.             }
  363.         }
  364.  
  365.         // Main sort phase: quicksort everything but the NaN's
  366.     sort1(a, fromIndex, n-fromIndex);
  367.  
  368.         // Postprocessing phase: change 0.0's to -0.0's as required
  369.         if (numNegZeros != 0) {
  370.             int j = binarySearch(a, 0.0f, fromIndex, n-1); // posn of ANY zero
  371.             do {
  372.                 j--;
  373.             } while (j>=0 && a[j]==0.0f);
  374.  
  375.             // j is now one less than the index of the FIRST zero
  376.             for (int k=0; k<numNegZeros; k++)
  377.                 a[++j] = -0.0f;
  378.         }
  379.     }
  380.  
  381.  
  382.     /*
  383.      * The code for each of the seven primitive types is largely identical.
  384.      * C'est la vie.
  385.      */
  386.  
  387.     /**
  388.      * Sorts the specified sub-array of longs into ascending order.
  389.      */
  390.     private static void sort1(long x[], int off, int len) {
  391.     // Insertion sort on smallest arrays
  392.     if (len < 7) {
  393.         for (int i=off; i<len+off; i++)
  394.         for (int j=i; j>off && x[j-1]>x[j]; j--)
  395.             swap(x, j, j-1);
  396.         return;
  397.     }
  398.  
  399.     // Choose a partition element, v
  400.     int m = off + len/2;       // Small arrays, middle element
  401.     if (len > 7) {
  402.         int l = off;
  403.         int n = off + len - 1;
  404.         if (len > 40) {        // Big arrays, pseudomedian of 9
  405.         int s = len/8;
  406.         l = med3(x, l,     l+s, l+2*s);
  407.         m = med3(x, m-s,   m,   m+s);
  408.         n = med3(x, n-2*s, n-s, n);
  409.         }
  410.         m = med3(x, l, m, n); // Mid-size, med of 3
  411.     }
  412.     long v = x[m];
  413.  
  414.     // Establish Invariant: v* (<v)* (>v)* v*
  415.     int a = off, b = a, c = off + len - 1, d = c;
  416.     while(true) {
  417.         while (b <= c && x[b] <= v) {
  418.         if (x[b] == v)
  419.             swap(x, a++, b);
  420.         b++;
  421.         }
  422.         while (c >= b && x[c] >= v) {
  423.         if (x[c] == v)
  424.             swap(x, c, d--);
  425.         c--;
  426.         }
  427.         if (b > c)
  428.         break;
  429.         swap(x, b++, c--);
  430.     }
  431.  
  432.     // Swap partition elements back to middle
  433.     int s, n = off + len;
  434.     s = Math.min(a-off, b-a  );  vecswap(x, off, b-s, s);
  435.     s = Math.min(d-c,   n-d-1);  vecswap(x, b,   n-s, s);
  436.  
  437.     // Recursively sort non-partition-elements
  438.     if ((s = b-a) > 1)
  439.         sort1(x, off, s);
  440.     if ((s = d-c) > 1)
  441.         sort1(x, n-s, s);
  442.     }
  443.  
  444.     /**
  445.      * Swaps x[a] with x[b].
  446.      */
  447.     private static void swap(long x[], int a, int b) {
  448.     long t = x[a];
  449.     x[a] = x[b];
  450.     x[b] = t;
  451.     }
  452.  
  453.     /**
  454.      * Swaps x[a .. (a+n-1)] with x[b .. (b+n-1)].
  455.      */
  456.     private static void vecswap(long x[], int a, int b, int n) {
  457.     for (int i=0; i<n; i++, a++, b++)
  458.         swap(x, a, b);
  459.     }
  460.  
  461.     /**
  462.      * Returns the index of the median of the three indexed longs.
  463.      */
  464.     private static int med3(long x[], int a, int b, int c) {
  465.     return (x[a] < x[b] ?
  466.         (x[b] < x[c] ? b : x[a] < x[c] ? c : a) :
  467.         (x[b] > x[c] ? b : x[a] > x[c] ? c : a));
  468.     }
  469.  
  470.     /**
  471.      * Sorts the specified sub-array of integers into ascending order.
  472.      */
  473.     private static void sort1(int x[], int off, int len) {
  474.     // Insertion sort on smallest arrays
  475.     if (len < 7) {
  476.         for (int i=off; i<len+off; i++)
  477.         for (int j=i; j>off && x[j-1]>x[j]; j--)
  478.             swap(x, j, j-1);
  479.         return;
  480.     }
  481.  
  482.     // Choose a partition element, v
  483.     int m = off + len/2;       // Small arrays, middle element
  484.     if (len > 7) {
  485.         int l = off;
  486.         int n = off + len - 1;
  487.         if (len > 40) {        // Big arrays, pseudomedian of 9
  488.         int s = len/8;
  489.         l = med3(x, l,     l+s, l+2*s);
  490.         m = med3(x, m-s,   m,   m+s);
  491.         n = med3(x, n-2*s, n-s, n);
  492.         }
  493.         m = med3(x, l, m, n); // Mid-size, med of 3
  494.     }
  495.     int v = x[m];
  496.  
  497.     // Establish Invariant: v* (<v)* (>v)* v*
  498.     int a = off, b = a, c = off + len - 1, d = c;
  499.     while(true) {
  500.         while (b <= c && x[b] <= v) {
  501.         if (x[b] == v)
  502.             swap(x, a++, b);
  503.         b++;
  504.         }
  505.         while (c >= b && x[c] >= v) {
  506.         if (x[c] == v)
  507.             swap(x, c, d--);
  508.         c--;
  509.         }
  510.         if (b > c)
  511.         break;
  512.         swap(x, b++, c--);
  513.     }
  514.  
  515.     // Swap partition elements back to middle
  516.     int s, n = off + len;
  517.     s = Math.min(a-off, b-a  );  vecswap(x, off, b-s, s);
  518.     s = Math.min(d-c,   n-d-1);  vecswap(x, b,   n-s, s);
  519.  
  520.     // Recursively sort non-partition-elements
  521.     if ((s = b-a) > 1)
  522.         sort1(x, off, s);
  523.     if ((s = d-c) > 1)
  524.         sort1(x, n-s, s);
  525.     }
  526.  
  527.     /**
  528.      * Swaps x[a] with x[b].
  529.      */
  530.     private static void swap(int x[], int a, int b) {
  531.     int t = x[a];
  532.     x[a] = x[b];
  533.     x[b] = t;
  534.     }
  535.  
  536.     /**
  537.      * Swaps x[a .. (a+n-1)] with x[b .. (b+n-1)].
  538.      */
  539.     private static void vecswap(int x[], int a, int b, int n) {
  540.     for (int i=0; i<n; i++, a++, b++)
  541.         swap(x, a, b);
  542.     }
  543.  
  544.     /**
  545.      * Returns the index of the median of the three indexed integers.
  546.      */
  547.     private static int med3(int x[], int a, int b, int c) {
  548.     return (x[a] < x[b] ?
  549.         (x[b] < x[c] ? b : x[a] < x[c] ? c : a) :
  550.         (x[b] > x[c] ? b : x[a] > x[c] ? c : a));
  551.     }
  552.  
  553.     /**
  554.      * Sorts the specified sub-array of shorts into ascending order.
  555.      */
  556.     private static void sort1(short x[], int off, int len) {
  557.     // Insertion sort on smallest arrays
  558.     if (len < 7) {
  559.         for (int i=off; i<len+off; i++)
  560.         for (int j=i; j>off && x[j-1]>x[j]; j--)
  561.             swap(x, j, j-1);
  562.         return;
  563.     }
  564.  
  565.     // Choose a partition element, v
  566.     int m = off + len/2;       // Small arrays, middle element
  567.     if (len > 7) {
  568.         int l = off;
  569.         int n = off + len - 1;
  570.         if (len > 40) {        // Big arrays, pseudomedian of 9
  571.         int s = len/8;
  572.         l = med3(x, l,     l+s, l+2*s);
  573.         m = med3(x, m-s,   m,   m+s);
  574.         n = med3(x, n-2*s, n-s, n);
  575.         }
  576.         m = med3(x, l, m, n); // Mid-size, med of 3
  577.     }
  578.     short v = x[m];
  579.  
  580.     // Establish Invariant: v* (<v)* (>v)* v*
  581.     int a = off, b = a, c = off + len - 1, d = c;
  582.     while(true) {
  583.         while (b <= c && x[b] <= v) {
  584.         if (x[b] == v)
  585.             swap(x, a++, b);
  586.         b++;
  587.         }
  588.         while (c >= b && x[c] >= v) {
  589.         if (x[c] == v)
  590.             swap(x, c, d--);
  591.         c--;
  592.         }
  593.         if (b > c)
  594.         break;
  595.         swap(x, b++, c--);
  596.     }
  597.  
  598.     // Swap partition elements back to middle
  599.     int s, n = off + len;
  600.     s = Math.min(a-off, b-a  );  vecswap(x, off, b-s, s);
  601.     s = Math.min(d-c,   n-d-1);  vecswap(x, b,   n-s, s);
  602.  
  603.     // Recursively sort non-partition-elements
  604.     if ((s = b-a) > 1)
  605.         sort1(x, off, s);
  606.     if ((s = d-c) > 1)
  607.         sort1(x, n-s, s);
  608.     }
  609.  
  610.     /**
  611.      * Swaps x[a] with x[b].
  612.      */
  613.     private static void swap(short x[], int a, int b) {
  614.     short t = x[a];
  615.     x[a] = x[b];
  616.     x[b] = t;
  617.     }
  618.  
  619.     /**
  620.      * Swaps x[a .. (a+n-1)] with x[b .. (b+n-1)].
  621.      */
  622.     private static void vecswap(short x[], int a, int b, int n) {
  623.     for (int i=0; i<n; i++, a++, b++)
  624.         swap(x, a, b);
  625.     }
  626.  
  627.     /**
  628.      * Returns the index of the median of the three indexed shorts.
  629.      */
  630.     private static int med3(short x[], int a, int b, int c) {
  631.     return (x[a] < x[b] ?
  632.         (x[b] < x[c] ? b : x[a] < x[c] ? c : a) :
  633.         (x[b] > x[c] ? b : x[a] > x[c] ? c : a));
  634.     }
  635.  
  636.  
  637.     /**
  638.      * Sorts the specified sub-array of chars into ascending order.
  639.      */
  640.     private static void sort1(char x[], int off, int len) {
  641.     // Insertion sort on smallest arrays
  642.     if (len < 7) {
  643.         for (int i=off; i<len+off; i++)
  644.         for (int j=i; j>off && x[j-1]>x[j]; j--)
  645.             swap(x, j, j-1);
  646.         return;
  647.     }
  648.  
  649.     // Choose a partition element, v
  650.     int m = off + len/2;       // Small arrays, middle element
  651.     if (len > 7) {
  652.         int l = off;
  653.         int n = off + len - 1;
  654.         if (len > 40) {        // Big arrays, pseudomedian of 9
  655.         int s = len/8;
  656.         l = med3(x, l,     l+s, l+2*s);
  657.         m = med3(x, m-s,   m,   m+s);
  658.         n = med3(x, n-2*s, n-s, n);
  659.         }
  660.         m = med3(x, l, m, n); // Mid-size, med of 3
  661.     }
  662.     char v = x[m];
  663.  
  664.     // Establish Invariant: v* (<v)* (>v)* v*
  665.     int a = off, b = a, c = off + len - 1, d = c;
  666.     while(true) {
  667.         while (b <= c && x[b] <= v) {
  668.         if (x[b] == v)
  669.             swap(x, a++, b);
  670.         b++;
  671.         }
  672.         while (c >= b && x[c] >= v) {
  673.         if (x[c] == v)
  674.             swap(x, c, d--);
  675.         c--;
  676.         }
  677.         if (b > c)
  678.         break;
  679.         swap(x, b++, c--);
  680.     }
  681.  
  682.     // Swap partition elements back to middle
  683.     int s, n = off + len;
  684.     s = Math.min(a-off, b-a  );  vecswap(x, off, b-s, s);
  685.     s = Math.min(d-c,   n-d-1);  vecswap(x, b,   n-s, s);
  686.  
  687.     // Recursively sort non-partition-elements
  688.     if ((s = b-a) > 1)
  689.         sort1(x, off, s);
  690.     if ((s = d-c) > 1)
  691.         sort1(x, n-s, s);
  692.     }
  693.  
  694.     /**
  695.      * Swaps x[a] with x[b].
  696.      */
  697.     private static void swap(char x[], int a, int b) {
  698.     char t = x[a];
  699.     x[a] = x[b];
  700.     x[b] = t;
  701.     }
  702.  
  703.     /**
  704.      * Swaps x[a .. (a+n-1)] with x[b .. (b+n-1)].
  705.      */
  706.     private static void vecswap(char x[], int a, int b, int n) {
  707.     for (int i=0; i<n; i++, a++, b++)
  708.         swap(x, a, b);
  709.     }
  710.  
  711.     /**
  712.      * Returns the index of the median of the three indexed chars.
  713.      */
  714.     private static int med3(char x[], int a, int b, int c) {
  715.     return (x[a] < x[b] ?
  716.         (x[b] < x[c] ? b : x[a] < x[c] ? c : a) :
  717.         (x[b] > x[c] ? b : x[a] > x[c] ? c : a));
  718.     }
  719.  
  720.  
  721.     /**
  722.      * Sorts the specified sub-array of bytes into ascending order.
  723.      */
  724.     private static void sort1(byte x[], int off, int len) {
  725.     // Insertion sort on smallest arrays
  726.     if (len < 7) {
  727.         for (int i=off; i<len+off; i++)
  728.         for (int j=i; j>off && x[j-1]>x[j]; j--)
  729.             swap(x, j, j-1);
  730.         return;
  731.     }
  732.  
  733.     // Choose a partition element, v
  734.     int m = off + len/2;       // Small arrays, middle element
  735.     if (len > 7) {
  736.         int l = off;
  737.         int n = off + len - 1;
  738.         if (len > 40) {        // Big arrays, pseudomedian of 9
  739.         int s = len/8;
  740.         l = med3(x, l,     l+s, l+2*s);
  741.         m = med3(x, m-s,   m,   m+s);
  742.         n = med3(x, n-2*s, n-s, n);
  743.         }
  744.         m = med3(x, l, m, n); // Mid-size, med of 3
  745.     }
  746.     byte v = x[m];
  747.  
  748.     // Establish Invariant: v* (<v)* (>v)* v*
  749.     int a = off, b = a, c = off + len - 1, d = c;
  750.     while(true) {
  751.         while (b <= c && x[b] <= v) {
  752.         if (x[b] == v)
  753.             swap(x, a++, b);
  754.         b++;
  755.         }
  756.         while (c >= b && x[c] >= v) {
  757.         if (x[c] == v)
  758.             swap(x, c, d--);
  759.         c--;
  760.         }
  761.         if (b > c)
  762.         break;
  763.         swap(x, b++, c--);
  764.     }
  765.  
  766.     // Swap partition elements back to middle
  767.     int s, n = off + len;
  768.     s = Math.min(a-off, b-a  );  vecswap(x, off, b-s, s);
  769.     s = Math.min(d-c,   n-d-1);  vecswap(x, b,   n-s, s);
  770.  
  771.     // Recursively sort non-partition-elements
  772.     if ((s = b-a) > 1)
  773.         sort1(x, off, s);
  774.     if ((s = d-c) > 1)
  775.         sort1(x, n-s, s);
  776.     }
  777.  
  778.     /**
  779.      * Swaps x[a] with x[b].
  780.      */
  781.     private static void swap(byte x[], int a, int b) {
  782.     byte t = x[a];
  783.     x[a] = x[b];
  784.     x[b] = t;
  785.     }
  786.  
  787.     /**
  788.      * Swaps x[a .. (a+n-1)] with x[b .. (b+n-1)].
  789.      */
  790.     private static void vecswap(byte x[], int a, int b, int n) {
  791.     for (int i=0; i<n; i++, a++, b++)
  792.         swap(x, a, b);
  793.     }
  794.  
  795.     /**
  796.      * Returns the index of the median of the three indexed bytes.
  797.      */
  798.     private static int med3(byte x[], int a, int b, int c) {
  799.     return (x[a] < x[b] ?
  800.         (x[b] < x[c] ? b : x[a] < x[c] ? c : a) :
  801.         (x[b] > x[c] ? b : x[a] > x[c] ? c : a));
  802.     }
  803.  
  804.  
  805.     /**
  806.      * Sorts the specified sub-array of doubles into ascending order.
  807.      */
  808.     private static void sort1(double x[], int off, int len) {
  809.     // Insertion sort on smallest arrays
  810.     if (len < 7) {
  811.         for (int i=off; i<len+off; i++)
  812.         for (int j=i; j>off && x[j-1]>x[j]; j--)
  813.             swap(x, j, j-1);
  814.         return;
  815.     }
  816.  
  817.     // Choose a partition element, v
  818.     int m = off + len/2;       // Small arrays, middle element
  819.     if (len > 7) {
  820.         int l = off;
  821.         int n = off + len - 1;
  822.         if (len > 40) {        // Big arrays, pseudomedian of 9
  823.         int s = len/8;
  824.         l = med3(x, l,     l+s, l+2*s);
  825.         m = med3(x, m-s,   m,   m+s);
  826.         n = med3(x, n-2*s, n-s, n);
  827.         }
  828.         m = med3(x, l, m, n); // Mid-size, med of 3
  829.     }
  830.     double v = x[m];
  831.  
  832.     // Establish Invariant: v* (<v)* (>v)* v*
  833.     int a = off, b = a, c = off + len - 1, d = c;
  834.     while(true) {
  835.         while (b <= c && x[b] <= v) {
  836.         if (x[b] == v)
  837.             swap(x, a++, b);
  838.         b++;
  839.         }
  840.         while (c >= b && x[c] >= v) {
  841.         if (x[c] == v)
  842.             swap(x, c, d--);
  843.         c--;
  844.         }
  845.         if (b > c)
  846.         break;
  847.         swap(x, b++, c--);
  848.     }
  849.  
  850.     // Swap partition elements back to middle
  851.     int s, n = off + len;
  852.     s = Math.min(a-off, b-a  );  vecswap(x, off, b-s, s);
  853.     s = Math.min(d-c,   n-d-1);  vecswap(x, b,   n-s, s);
  854.  
  855.     // Recursively sort non-partition-elements
  856.     if ((s = b-a) > 1)
  857.         sort1(x, off, s);
  858.     if ((s = d-c) > 1)
  859.         sort1(x, n-s, s);
  860.     }
  861.  
  862.     /**
  863.      * Swaps x[a] with x[b].
  864.      */
  865.     private static void swap(double x[], int a, int b) {
  866.     double t = x[a];
  867.     x[a] = x[b];
  868.     x[b] = t;
  869.     }
  870.  
  871.     /**
  872.      * Swaps x[a .. (a+n-1)] with x[b .. (b+n-1)].
  873.      */
  874.     private static void vecswap(double x[], int a, int b, int n) {
  875.     for (int i=0; i<n; i++, a++, b++)
  876.         swap(x, a, b);
  877.     }
  878.  
  879.     /**
  880.      * Returns the index of the median of the three indexed doubles.
  881.      */
  882.     private static int med3(double x[], int a, int b, int c) {
  883.     return (x[a] < x[b] ?
  884.         (x[b] < x[c] ? b : x[a] < x[c] ? c : a) :
  885.         (x[b] > x[c] ? b : x[a] > x[c] ? c : a));
  886.     }
  887.  
  888.  
  889.     /**
  890.      * Sorts the specified sub-array of floats into ascending order.
  891.      */
  892.     private static void sort1(float x[], int off, int len) {
  893.     // Insertion sort on smallest arrays
  894.     if (len < 7) {
  895.         for (int i=off; i<len+off; i++)
  896.         for (int j=i; j>off && x[j-1]>x[j]; j--)
  897.             swap(x, j, j-1);
  898.         return;
  899.     }
  900.  
  901.     // Choose a partition element, v
  902.     int m = off + len/2;       // Small arrays, middle element
  903.     if (len > 7) {
  904.         int l = off;
  905.         int n = off + len - 1;
  906.         if (len > 40) {        // Big arrays, pseudomedian of 9
  907.         int s = len/8;
  908.         l = med3(x, l,     l+s, l+2*s);
  909.         m = med3(x, m-s,   m,   m+s);
  910.         n = med3(x, n-2*s, n-s, n);
  911.         }
  912.         m = med3(x, l, m, n); // Mid-size, med of 3
  913.     }
  914.     float v = x[m];
  915.  
  916.     // Establish Invariant: v* (<v)* (>v)* v*
  917.     int a = off, b = a, c = off + len - 1, d = c;
  918.     while(true) {
  919.         while (b <= c && x[b] <= v) {
  920.         if (x[b] == v)
  921.             swap(x, a++, b);
  922.         b++;
  923.         }
  924.         while (c >= b && x[c] >= v) {
  925.         if (x[c] == v)
  926.             swap(x, c, d--);
  927.         c--;
  928.         }
  929.         if (b > c)
  930.         break;
  931.         swap(x, b++, c--);
  932.     }
  933.  
  934.     // Swap partition elements back to middle
  935.     int s, n = off + len;
  936.     s = Math.min(a-off, b-a  );  vecswap(x, off, b-s, s);
  937.     s = Math.min(d-c,   n-d-1);  vecswap(x, b,   n-s, s);
  938.  
  939.     // Recursively sort non-partition-elements
  940.     if ((s = b-a) > 1)
  941.         sort1(x, off, s);
  942.     if ((s = d-c) > 1)
  943.         sort1(x, n-s, s);
  944.     }
  945.  
  946.     /**
  947.      * Swaps x[a] with x[b].
  948.      */
  949.     private static void swap(float x[], int a, int b) {
  950.     float t = x[a];
  951.     x[a] = x[b];
  952.     x[b] = t;
  953.     }
  954.  
  955.     /**
  956.      * Swaps x[a .. (a+n-1)] with x[b .. (b+n-1)].
  957.      */
  958.     private static void vecswap(float x[], int a, int b, int n) {
  959.     for (int i=0; i<n; i++, a++, b++)
  960.         swap(x, a, b);
  961.     }
  962.  
  963.     /**
  964.      * Returns the index of the median of the three indexed floats.
  965.      */
  966.     private static int med3(float x[], int a, int b, int c) {
  967.     return (x[a] < x[b] ?
  968.         (x[b] < x[c] ? b : x[a] < x[c] ? c : a) :
  969.         (x[b] > x[c] ? b : x[a] > x[c] ? c : a));
  970.     }
  971.  
  972.  
  973.     /**
  974.      * Sorts the specified array of objects into ascending order, according to
  975.      * the <i>natural ordering</i> of its elements.  All elements in the array
  976.      * must implement the <tt>Comparable</tt> interface.  Furthermore, all
  977.      * elements in the array must be <i>mutually comparable</i> (that is,
  978.      * <tt>e1.compareTo(e2)</tt> must not throw a <tt>ClassCastException</tt>
  979.      * for any elements <tt>e1</tt> and <tt>e2</tt> in the array).<p>
  980.      *
  981.      * This sort is guaranteed to be <i>stable</i>:  equal elements will
  982.      * not be reordered as a result of the sort.<p>
  983.      *
  984.      * The sorting algorithm is a modified mergesort (in which the merge is
  985.      * omitted if the highest element in the low sublist is less than the
  986.      * lowest element in the high sublist).  This algorithm offers guaranteed
  987.      * n*log(n) performance, and can approach linear performance on nearly
  988.      * sorted lists.
  989.      * 
  990.      * @param a the array to be sorted.
  991.      * @throws  ClassCastException if the array contains elements that are not
  992.      *        <i>mutually comparable</i> (for example, strings and integers).
  993.      * @see Comparable
  994.      */
  995.     public static void sort(Object[] a) {
  996.         Object aux[] = (Object[])a.clone();
  997.         mergeSort(aux, a, 0, a.length);
  998.     }
  999.  
  1000.     /**
  1001.      * Sorts the specified range of the specified array of objects into
  1002.      * ascending order, according to the <i>natural ordering</i> of its
  1003.      * elements.  All elements in this range must implement the
  1004.      * <tt>Comparable</tt> interface.  Furthermore, all elements in this range
  1005.      * must be <i>mutually comparable</i> (that is, <tt>e1.compareTo(e2)</tt>
  1006.      * must not throw a <tt>ClassCastException</tt> for any elements
  1007.      * <tt>e1</tt> and <tt>e2</tt> in the array).<p>
  1008.      *
  1009.      * This sort is guaranteed to be <i>stable</i>:  equal elements will
  1010.      * not be reordered as a result of the sort.<p>
  1011.      *
  1012.      * The sorting algorithm is a modified mergesort (in which the merge is
  1013.      * omitted if the highest element in the low sublist is less than the
  1014.      * lowest element in the high sublist).  This algorithm offers guaranteed
  1015.      * n*log(n) performance, and can approach linear performance on nearly
  1016.      * sorted lists.
  1017.      * 
  1018.      * @param a the array to be sorted.
  1019.      * @param fromIndex the index of the first element (inclusive) to be
  1020.      *        sorted.
  1021.      * @param toIndex the index of the last element (exclusive) to be sorted.
  1022.      * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt>
  1023.      * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or
  1024.      *           <tt>toIndex > a.length</tt>
  1025.      * @throws    ClassCastException if the array contains elements that are
  1026.      *          not <i>mutually comparable</i> (for example, strings and
  1027.      *          integers).
  1028.      * @see Comparable
  1029.      */
  1030.     public static void sort(Object[] a, int fromIndex, int toIndex) {
  1031.         rangeCheck(a.length, fromIndex, toIndex);
  1032.         Object aux[] = (Object[])a.clone();  // Optimization opportunity
  1033.         mergeSort(aux, a, fromIndex, toIndex);
  1034.     }
  1035.  
  1036.     private static void mergeSort(Object src[], Object dest[],
  1037.                                   int low, int high) {
  1038.     int length = high - low;
  1039.  
  1040.     // Insertion sort on smallest arrays
  1041.     if (length < 7) {
  1042.         for (int i=low; i<high; i++)
  1043.         for (int j=i; j>low &&
  1044.                  ((Comparable)dest[j-1]).compareTo((Comparable)dest[j])>0; j--)
  1045.             swap(dest, j, j-1);
  1046.         return;
  1047.     }
  1048.  
  1049.         // Recursively sort halves of dest into src
  1050.         int mid = (low + high)/2;
  1051.         mergeSort(dest, src, low, mid);
  1052.         mergeSort(dest, src, mid, high);
  1053.  
  1054.         // If list is already sorted, just copy from src to dest.  This is an
  1055.         // optimization that results in faster sorts for nearly ordered lists.
  1056.         if (((Comparable)src[mid-1]).compareTo((Comparable)src[mid]) <= 0) {
  1057.            System.arraycopy(src, low, dest, low, length);
  1058.            return;
  1059.         }
  1060.  
  1061.         // Merge sorted halves (now in src) into dest
  1062.         for(int i = low, p = low, q = mid; i < high; i++) {
  1063.             if (q>=high || p<mid && ((Comparable)src[p]).compareTo(src[q])<=0)
  1064.                 dest[i] = src[p++];
  1065.             else
  1066.                 dest[i] = src[q++];
  1067.         }
  1068.     }
  1069.  
  1070.     /**
  1071.      * Swaps x[a] with x[b].
  1072.      */
  1073.     private static void swap(Object x[], int a, int b) {
  1074.     Object t = x[a];
  1075.     x[a] = x[b];
  1076.     x[b] = t;
  1077.     }
  1078.  
  1079.     /**
  1080.      * Sorts the specified array of objects according to the order induced by
  1081.      * the specified comparator.  All elements in the array must be
  1082.      * <i>mutually comparable</i> by the specified comparator (that is,
  1083.      * <tt>c.compare(e1, e2)</tt> must not throw a <tt>ClassCastException</tt>
  1084.      * for any elements <tt>e1</tt> and <tt>e2</tt> in the array).<p>
  1085.      *
  1086.      * This sort is guaranteed to be <i>stable</i>:  equal elements will
  1087.      * not be reordered as a result of the sort.<p>
  1088.      *
  1089.      * The sorting algorithm is a modified mergesort (in which the merge is
  1090.      * omitted if the highest element in the low sublist is less than the
  1091.      * lowest element in the high sublist).  This algorithm offers guaranteed
  1092.      * n*log(n) performance, and can approach linear performance on nearly
  1093.      * sorted lists.
  1094.      *
  1095.      * @param a the array to be sorted.
  1096.      * @param c the comparator to determine the order of the array.
  1097.      * @throws  ClassCastException if the array contains elements that are
  1098.      *        not <i>mutually comparable</i> using the specified comparator.
  1099.      * @see Comparator
  1100.      */
  1101.     public static void sort(Object[] a, Comparator c) {
  1102.         Object aux[] = (Object[])a.clone();
  1103.         mergeSort(aux, a, 0, a.length, c);
  1104.     }
  1105.  
  1106.     /**
  1107.      * Sorts the specified range of the specified array of objects according
  1108.      * to the order induced by the specified comparator.  All elements in the
  1109.      * range must be <i>mutually comparable</i> by the specified comparator
  1110.      * (that is, <tt>c.compare(e1, e2)</tt> must not throw a
  1111.      * <tt>ClassCastException</tt> for any elements <tt>e1</tt> and
  1112.      * <tt>e2</tt> in the range).<p>
  1113.      *
  1114.      * This sort is guaranteed to be <i>stable</i>:  equal elements will
  1115.      * not be reordered as a result of the sort.<p>
  1116.      *
  1117.      * The sorting algorithm is a modified mergesort (in which the merge is
  1118.      * omitted if the highest element in the low sublist is less than the
  1119.      * lowest element in the high sublist).  This algorithm offers guaranteed
  1120.      * n*log(n) performance, and can approach linear performance on nearly
  1121.      * sorted lists.
  1122.      *
  1123.      * @param a the array to be sorted.
  1124.      * @param fromIndex the index of the first element (inclusive) to be
  1125.      *        sorted.
  1126.      * @param toIndex the index of the last element (exclusive) to be sorted.
  1127.      * @param c the comparator to determine the order of the array.
  1128.      * @throws ClassCastException if the array contains elements that are not
  1129.      *           <i>mutually comparable</i> using the specified comparator.
  1130.      * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt>
  1131.      * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or
  1132.      *           <tt>toIndex > a.length</tt>
  1133.      * @see Comparator
  1134.      */
  1135.     public static void sort(Object[] a, int fromIndex, int toIndex,
  1136.                             Comparator c) {
  1137.         rangeCheck(a.length, fromIndex, toIndex);
  1138.         Object aux[] = (Object[])a.clone();
  1139.         mergeSort(aux, a, fromIndex, toIndex, c);
  1140.     }
  1141.  
  1142.     private static void mergeSort(Object src[], Object dest[],
  1143.                                   int low, int high, Comparator c) {
  1144.     int length = high - low;
  1145.  
  1146.     // Insertion sort on smallest arrays
  1147.     if (length < 7) {
  1148.         for (int i=low; i<high; i++)
  1149.         for (int j=i; j>low && c.compare(dest[j-1], dest[j])>0; j--)
  1150.             swap(dest, j, j-1);
  1151.         return;
  1152.     }
  1153.  
  1154.         // Recursively sort halves of dest into src
  1155.         int mid = (low + high)/2;
  1156.         mergeSort(dest, src, low, mid, c);
  1157.         mergeSort(dest, src, mid, high, c);
  1158.  
  1159.         // If list is already sorted, just copy from src to dest.  This is an
  1160.         // optimization that results in faster sorts for nearly ordered lists.
  1161.         if (c.compare(src[mid-1], src[mid]) <= 0) {
  1162.            System.arraycopy(src, low, dest, low, length);
  1163.            return;
  1164.         }
  1165.  
  1166.         // Merge sorted halves (now in src) into dest
  1167.         for(int i = low, p = low, q = mid; i < high; i++) {
  1168.             if (q>=high || p<mid && c.compare(src[p], src[q]) <= 0)
  1169.                 dest[i] = src[p++];
  1170.             else
  1171.                 dest[i] = src[q++];
  1172.         }
  1173.     }
  1174.  
  1175.     /**
  1176.      * Check that fromIndex and toIndex are in range, and throw an
  1177.      * appropriate exception if they aren't.
  1178.      */
  1179.     private static void rangeCheck(int arrayLen, int fromIndex, int toIndex) {
  1180.         if (fromIndex > toIndex)
  1181.             throw new IllegalArgumentException("fromIndex(" + fromIndex +
  1182.                        ") > toIndex(" + toIndex+")");
  1183.         if (fromIndex < 0)
  1184.             throw new ArrayIndexOutOfBoundsException(fromIndex);
  1185.         if (toIndex > arrayLen)
  1186.             throw new ArrayIndexOutOfBoundsException(toIndex);
  1187.     }
  1188.  
  1189.     // Searching
  1190.  
  1191.     /**
  1192.      * Searches the specified array of longs for the specified value using the
  1193.      * binary search algorithm.  The array <strong>must</strong> be sorted (as
  1194.      * by the <tt>sort</tt> method, above) prior to making this call.  If it
  1195.      * is not sorted, the results are undefined.  If the array contains
  1196.      * multiple elements with the specified value, there is no guarantee which
  1197.      * one will be found.
  1198.      *
  1199.      * @param a the array to be searched.
  1200.      * @param key the value to be searched for.
  1201.      * @return index of the search key, if it is contained in the list;
  1202.      *           otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
  1203.      *           <i>insertion point</i> is defined as the point at which the
  1204.      *           key would be inserted into the list: the index of the first
  1205.      *           element greater than the key, or <tt>list.size()</tt>, if all
  1206.      *           elements in the list are less than the specified key.  Note
  1207.      *           that this guarantees that the return value will be >= 0 if
  1208.      *           and only if the key is found.
  1209.      * @see #sort(long[])
  1210.      */
  1211.     public static int binarySearch(long[] a, long key) {
  1212.     int low = 0;
  1213.     int high = a.length-1;
  1214.  
  1215.     while (low <= high) {
  1216.         int mid =(low + high)/2;
  1217.         long midVal = a[mid];
  1218.  
  1219.         if (midVal < key)
  1220.         low = mid + 1;
  1221.         else if (midVal > key)
  1222.         high = mid - 1;
  1223.         else
  1224.         return mid; // key found
  1225.     }
  1226.     return -(low + 1);  // key not found.
  1227.     }
  1228.  
  1229.  
  1230.     /**
  1231.      * Searches the specified array of ints for the specified value using the
  1232.      * binary search algorithm.  The array <strong>must</strong> be sorted (as
  1233.      * by the <tt>sort</tt> method, above) prior to making this call.  If it
  1234.      * is not sorted, the results are undefined.  If the array contains
  1235.      * multiple elements with the specified value, there is no guarantee which
  1236.      * one will be found.
  1237.      *
  1238.      * @param a the array to be searched.
  1239.      * @param key the value to be searched for.
  1240.      * @return index of the search key, if it is contained in the list;
  1241.      *           otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
  1242.      *           <i>insertion point</i> is defined as the point at which the
  1243.      *           key would be inserted into the list: the index of the first
  1244.      *           element greater than the key, or <tt>list.size()</tt>, if all
  1245.      *           elements in the list are less than the specified key.  Note
  1246.      *           that this guarantees that the return value will be >= 0 if
  1247.      *           and only if the key is found.
  1248.      * @see #sort(int[])
  1249.      */
  1250.     public static int binarySearch(int[] a, int key) {
  1251.     int low = 0;
  1252.     int high = a.length-1;
  1253.  
  1254.     while (low <= high) {
  1255.         int mid =(low + high)/2;
  1256.         int midVal = a[mid];
  1257.  
  1258.         if (midVal < key)
  1259.         low = mid + 1;
  1260.         else if (midVal > key)
  1261.         high = mid - 1;
  1262.         else
  1263.         return mid; // key found
  1264.     }
  1265.     return -(low + 1);  // key not found.
  1266.     }
  1267.  
  1268.     /**
  1269.      * Searches the specified array of shorts for the specified value using
  1270.      * the binary search algorithm.  The array <strong>must</strong> be sorted
  1271.      * (as by the <tt>sort</tt> method, above) prior to making this call.  If
  1272.      * it is not sorted, the results are undefined.  If the array contains
  1273.      * multiple elements with the specified value, there is no guarantee which
  1274.      * one will be found.
  1275.      *
  1276.      * @param a the array to be searched.
  1277.      * @param key the value to be searched for.
  1278.      * @return index of the search key, if it is contained in the list;
  1279.      *           otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
  1280.      *           <i>insertion point</i> is defined as the point at which the
  1281.      *           key would be inserted into the list: the index of the first
  1282.      *           element greater than the key, or <tt>list.size()</tt>, if all
  1283.      *           elements in the list are less than the specified key.  Note
  1284.      *           that this guarantees that the return value will be >= 0 if
  1285.      *           and only if the key is found.
  1286.      * @see #sort(short[])
  1287.      */
  1288.     public static int binarySearch(short[] a, short key) {
  1289.     int low = 0;
  1290.     int high = a.length-1;
  1291.  
  1292.     while (low <= high) {
  1293.         int mid =(low + high)/2;
  1294.         short midVal = a[mid];
  1295.  
  1296.         if (midVal < key)
  1297.         low = mid + 1;
  1298.         else if (midVal > key)
  1299.         high = mid - 1;
  1300.         else
  1301.         return mid; // key found
  1302.     }
  1303.     return -(low + 1);  // key not found.
  1304.     }
  1305.  
  1306.     /**
  1307.      * Searches the specified array of chars for the specified value using the
  1308.      * binary search algorithm.  The array <strong>must</strong> be sorted (as
  1309.      * by the <tt>sort</tt> method, above) prior to making this call.  If it
  1310.      * is not sorted, the results are undefined.  If the array contains
  1311.      * multiple elements with the specified value, there is no guarantee which
  1312.      * one will be found.
  1313.      *
  1314.      * @param a the array to be searched.
  1315.      * @param key the value to be searched for.
  1316.      * @return index of the search key, if it is contained in the list;
  1317.      *           otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
  1318.      *           <i>insertion point</i> is defined as the point at which the
  1319.      *           key would be inserted into the list: the index of the first
  1320.      *           element greater than the key, or <tt>list.size()</tt>, if all
  1321.      *           elements in the list are less than the specified key.  Note
  1322.      *           that this guarantees that the return value will be >= 0 if
  1323.      *           and only if the key is found.
  1324.      * @see #sort(char[])
  1325.      */
  1326.     public static int binarySearch(char[] a, char key) {
  1327.     int low = 0;
  1328.     int high = a.length-1;
  1329.  
  1330.     while (low <= high) {
  1331.         int mid =(low + high)/2;
  1332.         char midVal = a[mid];
  1333.  
  1334.         if (midVal < key)
  1335.         low = mid + 1;
  1336.         else if (midVal > key)
  1337.         high = mid - 1;
  1338.         else
  1339.         return mid; // key found
  1340.     }
  1341.     return -(low + 1);  // key not found.
  1342.     }
  1343.  
  1344.     /**
  1345.      * Searches the specified array of bytes for the specified value using the
  1346.      * binary search algorithm.  The array <strong>must</strong> be sorted (as
  1347.      * by the <tt>sort</tt> method, above) prior to making this call.  If it
  1348.      * is not sorted, the results are undefined.  If the array contains
  1349.      * multiple elements with the specified value, there is no guarantee which
  1350.      * one will be found.
  1351.      *
  1352.      * @param a the array to be searched.
  1353.      * @param key the value to be searched for.
  1354.      * @return index of the search key, if it is contained in the list;
  1355.      *           otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
  1356.      *           <i>insertion point</i> is defined as the point at which the
  1357.      *           key would be inserted into the list: the index of the first
  1358.      *           element greater than the key, or <tt>list.size()</tt>, if all
  1359.      *           elements in the list are less than the specified key.  Note
  1360.      *           that this guarantees that the return value will be >= 0 if
  1361.      *           and only if the key is found.
  1362.      * @see #sort(byte[])
  1363.      */
  1364.     public static int binarySearch(byte[] a, byte key) {
  1365.     int low = 0;
  1366.     int high = a.length-1;
  1367.  
  1368.     while (low <= high) {
  1369.         int mid =(low + high)/2;
  1370.         byte midVal = a[mid];
  1371.  
  1372.         if (midVal < key)
  1373.         low = mid + 1;
  1374.         else if (midVal > key)
  1375.         high = mid - 1;
  1376.         else
  1377.         return mid; // key found
  1378.     }
  1379.     return -(low + 1);  // key not found.
  1380.     }
  1381.  
  1382.     /**
  1383.      * Searches the specified array of doubles for the specified value using
  1384.      * the binary search algorithm.  The array <strong>must</strong> be sorted
  1385.      * (as by the <tt>sort</tt> method, above) prior to making this call.  If
  1386.      * it is not sorted, the results are undefined.  If the array contains
  1387.      * multiple elements with the specified value, there is no guarantee which
  1388.      * one will be found.
  1389.      *
  1390.      * @param a the array to be searched.
  1391.      * @param key the value to be searched for.
  1392.      * @return index of the search key, if it is contained in the list;
  1393.      *           otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
  1394.      *           <i>insertion point</i> is defined as the point at which the
  1395.      *           key would be inserted into the list: the index of the first
  1396.      *           element greater than the key, or <tt>list.size()</tt>, if all
  1397.      *           elements in the list are less than the specified key.  Note
  1398.      *           that this guarantees that the return value will be >= 0 if
  1399.      *           and only if the key is found.
  1400.      * @see #sort(double[])
  1401.      */
  1402.     public static int binarySearch(double[] a, double key) {
  1403.         return binarySearch(a, key, 0, a.length-1);
  1404.     }
  1405.  
  1406.     private static int binarySearch(double[] a, double key, int low,int high) {
  1407.     while (low <= high) {
  1408.         int mid =(low + high)/2;
  1409.         double midVal = a[mid];
  1410.  
  1411.             int cmp;
  1412.             if (midVal < key) {
  1413.                 cmp = -1;   // Neither val is NaN, thisVal is smaller
  1414.             } else if (midVal > key) {
  1415.                 cmp = 1;    // Neither val is NaN, thisVal is larger
  1416.             } else {
  1417.                 long midBits = Double.doubleToLongBits(midVal);
  1418.                 long keyBits = Double.doubleToLongBits(key);
  1419.                 cmp = (midBits == keyBits ?  0 : // Values are equal
  1420.                        (midBits < keyBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)
  1421.                         1));                     // (0.0, -0.0) or (NaN, !NaN)
  1422.             }
  1423.  
  1424.         if (cmp < 0)
  1425.         low = mid + 1;
  1426.         else if (cmp > 0)
  1427.         high = mid - 1;
  1428.         else
  1429.         return mid; // key found
  1430.     }
  1431.     return -(low + 1);  // key not found.
  1432.     }
  1433.  
  1434.     /**
  1435.      * Searches the specified array of floats for the specified value using
  1436.      * the binary search algorithm.  The array <strong>must</strong> be sorted
  1437.      * (as by the <tt>sort</tt> method, above) prior to making this call.  If
  1438.      * it is not sorted, the results are undefined.  If the array contains
  1439.      * multiple elements with the specified value, there is no guarantee which
  1440.      * one will be found.
  1441.      *
  1442.      * @param a the array to be searched.
  1443.      * @param key the value to be searched for.
  1444.      * @return index of the search key, if it is contained in the list;
  1445.      *           otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
  1446.      *           <i>insertion point</i> is defined as the point at which the
  1447.      *           key would be inserted into the list: the index of the first
  1448.      *           element greater than the key, or <tt>list.size()</tt>, if all
  1449.      *           elements in the list are less than the specified key.  Note
  1450.      *           that this guarantees that the return value will be >= 0 if
  1451.      *           and only if the key is found.
  1452.      * @see #sort(float[])
  1453.      */
  1454.     public static int binarySearch(float[] a, float key) {
  1455.         return binarySearch(a, key, 0, a.length-1);
  1456.     }
  1457.  
  1458.     private static int binarySearch(float[] a, float key, int low,int high) {
  1459.     while (low <= high) {
  1460.         int mid =(low + high)/2;
  1461.         float midVal = a[mid];
  1462.  
  1463.             int cmp;
  1464.             if (midVal < key) {
  1465.                 cmp = -1;   // Neither val is NaN, thisVal is smaller
  1466.             } else if (midVal > key) {
  1467.                 cmp = 1;    // Neither val is NaN, thisVal is larger
  1468.             } else {
  1469.                 int midBits = Float.floatToIntBits(midVal);
  1470.                 int keyBits = Float.floatToIntBits(key);
  1471.                 cmp = (midBits == keyBits ?  0 : // Values are equal
  1472.                        (midBits < keyBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)
  1473.                         1));                     // (0.0, -0.0) or (NaN, !NaN)
  1474.             }
  1475.  
  1476.         if (cmp < 0)
  1477.         low = mid + 1;
  1478.         else if (cmp > 0)
  1479.         high = mid - 1;
  1480.         else
  1481.         return mid; // key found
  1482.     }
  1483.     return -(low + 1);  // key not found.
  1484.     }
  1485.  
  1486.  
  1487.     /**
  1488.      * Searches the specified array for the specified object using the binary
  1489.      * search algorithm.  The array must be sorted into ascending order
  1490.      * according to the <i>natural ordering</i> of its elements (as by
  1491.      * <tt>Sort(Object[]</tt>), above) prior to making this call.  If it is
  1492.      * not sorted, the results are undefined.  If the array contains multiple
  1493.      * elements equal to the specified object, there is no guarantee which
  1494.      * one will be found.
  1495.      *
  1496.      * @param a the array to be searched.
  1497.      * @param key the value to be searched for.
  1498.      * @return index of the search key, if it is contained in the list;
  1499.      *           otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
  1500.      *           <i>insertion point</i> is defined as the point at which the
  1501.      *           key would be inserted into the list: the index of the first
  1502.      *           element greater than the key, or <tt>list.size()</tt>, if all
  1503.      *           elements in the list are less than the specified key.  Note
  1504.      *           that this guarantees that the return value will be >= 0 if
  1505.      *           and only if the key is found.
  1506.      * @throws ClassCastException if the array contains elements that are not
  1507.      *           <i>mutually comparable</i> (for example, strings and integers),
  1508.      *         or the search key in not mutually comparable with the elements
  1509.      *         of the array.
  1510.      * @see Comparable
  1511.      * @see #sort(Object[])
  1512.      */
  1513.     public static int binarySearch(Object[] a, Object key) {
  1514.     int low = 0;
  1515.     int high = a.length-1;
  1516.  
  1517.     while (low <= high) {
  1518.         int mid =(low + high)/2;
  1519.         Object midVal = a[mid];
  1520.         int cmp = ((Comparable)midVal).compareTo(key);
  1521.  
  1522.         if (cmp < 0)
  1523.         low = mid + 1;
  1524.         else if (cmp > 0)
  1525.         high = mid - 1;
  1526.         else
  1527.         return mid; // key found
  1528.     }
  1529.     return -(low + 1);  // key not found.
  1530.     }
  1531.  
  1532.     /**
  1533.      * Searches the specified array for the specified object using the binary
  1534.      * search algorithm.  The array must be sorted into ascending order
  1535.      * according to the specified comparator (as by the <tt>Sort(Object[],
  1536.      * Comparator)</tt> method, above), prior to making this call.  If it is
  1537.      * not sorted, the results are undefined.  If the array contains multiple
  1538.      * elements equal to the specified object, there is no guarantee which one
  1539.      * will be found.
  1540.      *
  1541.      * @param a the array to be searched.
  1542.      * @param key the value to be searched for.
  1543.      * @param c the comparator by which the array is ordered.
  1544.      * @return index of the search key, if it is contained in the list;
  1545.      *           otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
  1546.      *           <i>insertion point</i> is defined as the point at which the
  1547.      *           key would be inserted into the list: the index of the first
  1548.      *           element greater than the key, or <tt>list.size()</tt>, if all
  1549.      *           elements in the list are less than the specified key.  Note
  1550.      *           that this guarantees that the return value will be >= 0 if
  1551.      *           and only if the key is found.
  1552.      * @throws ClassCastException if the array contains elements that are not
  1553.      *           <i>mutually comparable</i> using the specified comparator,
  1554.      *           or the search key in not mutually comparable with the
  1555.      *           elements of the array using this comparator.
  1556.      * @see Comparable
  1557.      * @see #sort(Object[], Comparator)
  1558.      */
  1559.     public static int binarySearch(Object[] a, Object key, Comparator c) {
  1560.     int low = 0;
  1561.     int high = a.length-1;
  1562.  
  1563.     while (low <= high) {
  1564.         int mid =(low + high)/2;
  1565.         Object midVal = a[mid];
  1566.         int cmp = c.compare(midVal, key);
  1567.  
  1568.         if (cmp < 0)
  1569.         low = mid + 1;
  1570.         else if (cmp > 0)
  1571.         high = mid - 1;
  1572.         else
  1573.         return mid; // key found
  1574.     }
  1575.     return -(low + 1);  // key not found.
  1576.     }
  1577.  
  1578.  
  1579.     // Equality Testing
  1580.  
  1581.     /**
  1582.      * Returns <tt>true</tt> if the two specified arrays of longs are
  1583.      * <i>equal</i> to one another.  Two arrays are considered equal if both
  1584.      * arrays contain the same number of elements, and all corresponding pairs
  1585.      * of elements in the two arrays are equal.  In other words, two arrays
  1586.      * are equal if they contain the same elements in the same order.  Also,
  1587.      * two array references are considered equal if both are <tt>null</tt>.<p>
  1588.      *
  1589.      * @param a one array to be tested for equality.
  1590.      * @param a2 the other array to be tested for equality.
  1591.      * @return <tt>true</tt> if the two arrays are equal.
  1592.      */
  1593.     public static boolean equals(long[] a, long[] a2) {
  1594.         if (a==a2)
  1595.             return true;
  1596.         if (a==null || a2==null)
  1597.             return false;
  1598.  
  1599.         int length = a.length;
  1600.         if (a2.length != length)
  1601.             return false;
  1602.  
  1603.         for (int i=0; i<length; i++)
  1604.             if (a[i] != a2[i])
  1605.                 return false;
  1606.  
  1607.         return true;
  1608.     }
  1609.  
  1610.     /**
  1611.      * Returns <tt>true</tt> if the two specified arrays of ints are
  1612.      * <i>equal</i> to one another.  Two arrays are considered equal if both
  1613.      * arrays contain the same number of elements, and all corresponding pairs
  1614.      * of elements in the two arrays are equal.  In other words, two arrays
  1615.      * are equal if they contain the same elements in the same order.  Also,
  1616.      * two array references are considered equal if both are <tt>null</tt>.<p>
  1617.      *
  1618.      * @param a one array to be tested for equality.
  1619.      * @param a2 the other array to be tested for equality.
  1620.      * @return <tt>true</tt> if the two arrays are equal.
  1621.      */
  1622.     public static boolean equals(int[] a, int[] a2) {
  1623.         if (a==a2)
  1624.             return true;
  1625.         if (a==null || a2==null)
  1626.             return false;
  1627.  
  1628.         int length = a.length;
  1629.         if (a2.length != length)
  1630.             return false;
  1631.  
  1632.         for (int i=0; i<length; i++)
  1633.             if (a[i] != a2[i])
  1634.                 return false;
  1635.  
  1636.         return true;
  1637.     }
  1638.  
  1639.     /**
  1640.      * Returns <tt>true</tt> if the two specified arrays of shorts are
  1641.      * <i>equal</i> to one another.  Two arrays are considered equal if both
  1642.      * arrays contain the same number of elements, and all corresponding pairs
  1643.      * of elements in the two arrays are equal.  In other words, two arrays
  1644.      * are equal if they contain the same elements in the same order.  Also,
  1645.      * two array references are considered equal if both are <tt>null</tt>.<p>
  1646.      *
  1647.      * @param a one array to be tested for equality.
  1648.      * @param a2 the other array to be tested for equality.
  1649.      * @return <tt>true</tt> if the two arrays are equal.
  1650.      */
  1651.     public static boolean equals(short[] a, short a2[]) {
  1652.         if (a==a2)
  1653.             return true;
  1654.         if (a==null || a2==null)
  1655.             return false;
  1656.  
  1657.         int length = a.length;
  1658.         if (a2.length != length)
  1659.             return false;
  1660.  
  1661.         for (int i=0; i<length; i++)
  1662.             if (a[i] != a2[i])
  1663.                 return false;
  1664.  
  1665.         return true;
  1666.     }
  1667.  
  1668.     /**
  1669.      * Returns <tt>true</tt> if the two specified arrays of chars are
  1670.      * <i>equal</i> to one another.  Two arrays are considered equal if both
  1671.      * arrays contain the same number of elements, and all corresponding pairs
  1672.      * of elements in the two arrays are equal.  In other words, two arrays
  1673.      * are equal if they contain the same elements in the same order.  Also,
  1674.      * two array references are considered equal if both are <tt>null</tt>.<p>
  1675.      *
  1676.      * @param a one array to be tested for equality.
  1677.      * @param a2 the other array to be tested for equality.
  1678.      * @return <tt>true</tt> if the two arrays are equal.
  1679.      */
  1680.     public static boolean equals(char[] a, char[] a2) {
  1681.         if (a==a2)
  1682.             return true;
  1683.         if (a==null || a2==null)
  1684.             return false;
  1685.  
  1686.         int length = a.length;
  1687.         if (a2.length != length)
  1688.             return false;
  1689.  
  1690.         for (int i=0; i<length; i++)
  1691.             if (a[i] != a2[i])
  1692.                 return false;
  1693.  
  1694.         return true;
  1695.     }
  1696.  
  1697.     /**
  1698.      * Returns <tt>true</tt> if the two specified arrays of bytes are
  1699.      * <i>equal</i> to one another.  Two arrays are considered equal if both
  1700.      * arrays contain the same number of elements, and all corresponding pairs
  1701.      * of elements in the two arrays are equal.  In other words, two arrays
  1702.      * are equal if they contain the same elements in the same order.  Also,
  1703.      * two array references are considered equal if both are <tt>null</tt>.<p>
  1704.      *
  1705.      * @param a one array to be tested for equality.
  1706.      * @param a2 the other array to be tested for equality.
  1707.      * @return <tt>true</tt> if the two arrays are equal.
  1708.      */
  1709.     public static boolean equals(byte[] a, byte[] a2) {
  1710.         if (a==a2)
  1711.             return true;
  1712.         if (a==null || a2==null)
  1713.             return false;
  1714.  
  1715.         int length = a.length;
  1716.         if (a2.length != length)
  1717.             return false;
  1718.  
  1719.         for (int i=0; i<length; i++)
  1720.             if (a[i] != a2[i])
  1721.                 return false;
  1722.  
  1723.         return true;
  1724.     }
  1725.  
  1726.     /**
  1727.      * Returns <tt>true</tt> if the two specified arrays of equals are
  1728.      * <i>equal</i> to one another.  Two arrays are considered equal if both
  1729.      * arrays contain the same number of elements, and all corresponding pairs
  1730.      * of elements in the two arrays are equal.  In other words, two arrays
  1731.      * are equal if they contain the same elements in the same order.  Also,
  1732.      * two array references are considered equal if both are <tt>null</tt>.<p>
  1733.      *
  1734.      * @param a one array to be tested for equality.
  1735.      * @param a2 the other array to be tested for equality.
  1736.      * @return <tt>true</tt> if the two arrays are equal.
  1737.      */
  1738.     public static boolean equals(boolean[] a, boolean[] a2) {
  1739.         if (a==a2)
  1740.             return true;
  1741.         if (a==null || a2==null)
  1742.             return false;
  1743.  
  1744.         int length = a.length;
  1745.         if (a2.length != length)
  1746.             return false;
  1747.  
  1748.         for (int i=0; i<length; i++)
  1749.             if (a[i] != a2[i])
  1750.                 return false;
  1751.  
  1752.         return true;
  1753.     }
  1754.  
  1755.     /**
  1756.      * Returns <tt>true</tt> if the two specified arrays of doubles are
  1757.      * <i>equal</i> to one another.  Two arrays are considered equal if both
  1758.      * arrays contain the same number of elements, and all corresponding pairs
  1759.      * of elements in the two arrays are equal.  In other words, two arrays
  1760.      * are equal if they contain the same elements in the same order.  Also,
  1761.      * two array references are considered equal if both are <tt>null</tt>.<p>
  1762.      *
  1763.      * Two doubles <tt>d1</tt> and <tt>d2</tt> are considered equal if:
  1764.      * <pre>    <tt>new Double(d1).equals(new Double(d2))</tt></pre>
  1765.      * (Unlike the <tt>==</tt> operator, this method considers
  1766.      * <tt>NaN</tt> equals to itself, and 0.0d unequal to -0.0d.)
  1767.      *
  1768.      * @param a one array to be tested for equality.
  1769.      * @param a2 the other array to be tested for equality.
  1770.      * @return <tt>true</tt> if the two arrays are equal.
  1771.      * @see Double#equals(Double)
  1772.      */
  1773.     public static boolean equals(double[] a, double[] a2) {
  1774.         if (a==a2)
  1775.             return true;
  1776.         if (a==null || a2==null)
  1777.             return false;
  1778.  
  1779.         int length = a.length;
  1780.         if (a2.length != length)
  1781.             return false;
  1782.  
  1783.         for (int i=0; i<length; i++)
  1784.         if (Double.doubleToLongBits(a[i])!=Double.doubleToLongBits(a2[i]))
  1785.                 return false;
  1786.  
  1787.         return true;
  1788.     }
  1789.  
  1790.     /**
  1791.      * Returns <tt>true</tt> if the two specified arrays of floats are
  1792.      * <i>equal</i> to one another.  Two arrays are considered equal if both
  1793.      * arrays contain the same number of elements, and all corresponding pairs
  1794.      * of elements in the two arrays are equal.  In other words, two arrays
  1795.      * are equal if they contain the same elements in the same order.  Also,
  1796.      * two array references are considered equal if both are <tt>null</tt>.<p>
  1797.      *
  1798.      * Two doubles <tt>d1</tt> and <tt>d2</tt> are considered equal if:
  1799.      * <pre>    <tt>new Double(d1).equals(new Double(d2))</tt></pre>
  1800.      * (Unlike the <tt>==</tt> operator, this method considers
  1801.      * <tt>NaN</tt> equals to itself, and 0.0d unequal to -0.0d.)
  1802.      *
  1803.      * @param a one array to be tested for equality.
  1804.      * @param a2 the other array to be tested for equality.
  1805.      * @return <tt>true</tt> if the two arrays are equal.
  1806.      * @see Double#equals(Double)
  1807.      */
  1808.     public static boolean equals(float[] a, float[] a2) {
  1809.         if (a==a2)
  1810.             return true;
  1811.         if (a==null || a2==null)
  1812.             return false;
  1813.  
  1814.         int length = a.length;
  1815.         if (a2.length != length)
  1816.             return false;
  1817.  
  1818.         for (int i=0; i<length; i++)
  1819.         if (Float.floatToIntBits(a[i])!=Float.floatToIntBits(a2[i]))
  1820.                 return false;
  1821.  
  1822.         return true;
  1823.     }
  1824.  
  1825.  
  1826.     /**
  1827.      * Returns <tt>true</tt> if the two specified arrays of Objects are
  1828.      * <i>equal</i> to one another.  The two arrays are considered equal if
  1829.      * both arrays contain the same number of elements, and all corresponding
  1830.      * pairs of elements in the two arrays are equal.  Two objects <tt>e1</tt>
  1831.      * and <tt>e2</tt> are considered <i>equal</i> if <tt>(e1==null ? e2==null
  1832.      * : e1.equals(e2))</tt>.  In other words, the two arrays are equal if
  1833.      * they contain the same elements in the same order.  Also, two array
  1834.      * references are considered equal if both are <tt>null</tt>.<p>
  1835.      *
  1836.      * @param a one array to be tested for equality.
  1837.      * @param a2 the other array to be tested for equality.
  1838.      * @return <tt>true</tt> if the two arrays are equal.
  1839.      */
  1840.     public static boolean equals(Object[] a, Object[] a2) {
  1841.         if (a==a2)
  1842.             return true;
  1843.         if (a==null || a2==null)
  1844.             return false;
  1845.  
  1846.         int length = a.length;
  1847.         if (a2.length != length)
  1848.             return false;
  1849.  
  1850.         for (int i=0; i<length; i++) {
  1851.             Object o1 = a[i];
  1852.             Object o2 = a2[i];
  1853.             if (!(o1==null ? o2==null : o1.equals(o2)))
  1854.                 return false;
  1855.         }
  1856.  
  1857.         return true;
  1858.     }
  1859.  
  1860.  
  1861.     // Filling
  1862.  
  1863.     /**
  1864.      * Assigns the specified long value to each element of the specified array
  1865.      * of longs.
  1866.      *
  1867.      * @param a the array to be filled.
  1868.      * @param val the value to be stored in all elements of the array.
  1869.      */
  1870.     public static void fill(long[] a, long val) {
  1871.         fill(a, 0, a.length, val);
  1872.     }
  1873.  
  1874.     /**
  1875.      * Assigns the specified long value to each element of the specified 
  1876.      * range of the specified array of longs.
  1877.      *
  1878.      * @param a the array to be filled.
  1879.      * @param fromIndex the index of the first element (inclusive) to be
  1880.      *        filled with the specified value.
  1881.      * @param toIndex the index of the last element (exclusive) to be
  1882.      *        filled with the specified value.
  1883.      * @param val the value to be stored in all elements of the array.
  1884.      * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt>
  1885.      * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or
  1886.      *           <tt>toIndex > a.length</tt>
  1887.      */
  1888.     public static void fill(long[] a, int fromIndex, int toIndex, long val) {
  1889.         rangeCheck(a.length, fromIndex, toIndex);
  1890.         for (int i=fromIndex; i<toIndex; i++)
  1891.             a[i] = val;
  1892.     }
  1893.  
  1894.     /**
  1895.      * Assigns the specified int value to each element of the specified array
  1896.      * of ints.
  1897.      *
  1898.      * @param a the array to be filled.
  1899.      * @param val the value to be stored in all elements of the array.
  1900.      */
  1901.     public static void fill(int[] a, int val) {
  1902.         fill(a, 0, a.length, val);
  1903.     }
  1904.  
  1905.     /**
  1906.      * Assigns the specified int value to each element of the specified 
  1907.      * range of the specified array of ints.
  1908.      *
  1909.      * @param a the array to be filled.
  1910.      * @param fromIndex the index of the first element (inclusive) to be
  1911.      *        filled with the specified value.
  1912.      * @param toIndex the index of the last element (exclusive) to be
  1913.      *        filled with the specified value.
  1914.      * @param val the value to be stored in all elements of the array.
  1915.      * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt>
  1916.      * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or
  1917.      *           <tt>toIndex > a.length</tt>
  1918.      */
  1919.     public static void fill(int[] a, int fromIndex, int toIndex, int val) {
  1920.         rangeCheck(a.length, fromIndex, toIndex);
  1921.         for (int i=fromIndex; i<toIndex; i++)
  1922.             a[i] = val;
  1923.     }
  1924.  
  1925.     /**
  1926.      * Assigns the specified short value to each element of the specified array
  1927.      * of shorts.
  1928.      *
  1929.      * @param a the array to be filled.
  1930.      * @param val the value to be stored in all elements of the array.
  1931.      */
  1932.     public static void fill(short[] a, short val) {
  1933.         fill(a, 0, a.length, val);
  1934.     }
  1935.  
  1936.     /**
  1937.      * Assigns the specified short value to each element of the specified 
  1938.      * range of the specified array of shorts.
  1939.      *
  1940.      * @param a the array to be filled.
  1941.      * @param fromIndex the index of the first element (inclusive) to be
  1942.      *        filled with the specified value.
  1943.      * @param toIndex the index of the last element (exclusive) to be
  1944.      *        filled with the specified value.
  1945.      * @param val the value to be stored in all elements of the array.
  1946.      * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt>
  1947.      * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or
  1948.      *           <tt>toIndex > a.length</tt>
  1949.      */
  1950.     public static void fill(short[] a, int fromIndex, int toIndex, short val) {
  1951.         rangeCheck(a.length, fromIndex, toIndex);
  1952.         for (int i=fromIndex; i<toIndex; i++)
  1953.             a[i] = val;
  1954.     }
  1955.  
  1956.     /**
  1957.      * Assigns the specified char value to each element of the specified array
  1958.      * of chars.
  1959.      *
  1960.      * @param a the array to be filled.
  1961.      * @param val the value to be stored in all elements of the array.
  1962.      */
  1963.     public static void fill(char[] a, char val) {
  1964.         fill(a, 0, a.length, val);
  1965.     }
  1966.  
  1967.     /**
  1968.      * Assigns the specified char value to each element of the specified 
  1969.      * range of the specified array of chars.
  1970.      *
  1971.      * @param a the array to be filled.
  1972.      * @param fromIndex the index of the first element (inclusive) to be
  1973.      *        filled with the specified value.
  1974.      * @param toIndex the index of the last element (exclusive) to be
  1975.      *        filled with the specified value.
  1976.      * @param val the value to be stored in all elements of the array.
  1977.      * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt>
  1978.      * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or
  1979.      *           <tt>toIndex > a.length</tt>
  1980.      */
  1981.     public static void fill(char[] a, int fromIndex, int toIndex, char val) {
  1982.         rangeCheck(a.length, fromIndex, toIndex);
  1983.         for (int i=fromIndex; i<toIndex; i++)
  1984.             a[i] = val;
  1985.     }
  1986.  
  1987.     /**
  1988.      * Assigns the specified byte value to each element of the specified array
  1989.      * of bytes.
  1990.      *
  1991.      * @param a the array to be filled.
  1992.      * @param val the value to be stored in all elements of the array.
  1993.      */
  1994.     public static void fill(byte[] a, byte val) {
  1995.         fill(a, 0, a.length, val);
  1996.     }
  1997.  
  1998.     /**
  1999.      * Assigns the specified byte value to each element of the specified 
  2000.      * range of the specified array of bytes.
  2001.      *
  2002.      * @param a the array to be filled.
  2003.      * @param fromIndex the index of the first element (inclusive) to be
  2004.      *        filled with the specified value.
  2005.      * @param toIndex the index of the last element (exclusive) to be
  2006.      *        filled with the specified value.
  2007.      * @param val the value to be stored in all elements of the array.
  2008.      * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt>
  2009.      * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or
  2010.      *           <tt>toIndex > a.length</tt>
  2011.      */
  2012.     public static void fill(byte[] a, int fromIndex, int toIndex, byte val) {
  2013.         rangeCheck(a.length, fromIndex, toIndex);
  2014.         for (int i=fromIndex; i<toIndex; i++)
  2015.             a[i] = val;
  2016.     }
  2017.  
  2018.     /**
  2019.      * Assigns the specified boolean value to each element of the specified
  2020.      * array of booleans.
  2021.      *
  2022.      * @param a the array to be filled.
  2023.      * @param val the value to be stored in all elements of the array.
  2024.      */
  2025.     public static void fill(boolean[] a, boolean val) {
  2026.         fill(a, 0, a.length, val);
  2027.     }
  2028.  
  2029.     /**
  2030.      * Assigns the specified boolean value to each element of the specified 
  2031.      * range of the specified array of booleans.
  2032.      *
  2033.      * @param a the array to be filled.
  2034.      * @param fromIndex the index of the first element (inclusive) to be
  2035.      *        filled with the specified value.
  2036.      * @param toIndex the index of the last element (exclusive) to be
  2037.      *        filled with the specified value.
  2038.      * @param val the value to be stored in all elements of the array.
  2039.      * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt>
  2040.      * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or
  2041.      *           <tt>toIndex > a.length</tt>
  2042.      */
  2043.     public static void fill(boolean[] a, int fromIndex, int toIndex,
  2044.                             boolean val) {
  2045.         rangeCheck(a.length, fromIndex, toIndex);
  2046.         for (int i=fromIndex; i<toIndex; i++)
  2047.             a[i] = val;
  2048.     }
  2049.  
  2050.     /**
  2051.      * Assigns the specified double value to each element of the specified
  2052.      * array of doubles.
  2053.      *
  2054.      * @param a the array to be filled.
  2055.      * @param val the value to be stored in all elements of the array.
  2056.      */
  2057.     public static void fill(double[] a, double val) {
  2058.         fill(a, 0, a.length, val);
  2059.     }
  2060.  
  2061.     /**
  2062.      * Assigns the specified double value to each element of the specified 
  2063.      * range of the specified array of doubles.
  2064.      *
  2065.      * @param a the array to be filled.
  2066.      * @param fromIndex the index of the first element (inclusive) to be
  2067.      *        filled with the specified value.
  2068.      * @param toIndex the index of the last element (exclusive) to be
  2069.      *        filled with the specified value.
  2070.      * @param val the value to be stored in all elements of the array.
  2071.      * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt>
  2072.      * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or
  2073.      *           <tt>toIndex > a.length</tt>
  2074.      */
  2075.     public static void fill(double[] a, int fromIndex, int toIndex,double val){
  2076.         rangeCheck(a.length, fromIndex, toIndex);
  2077.         for (int i=fromIndex; i<toIndex; i++)
  2078.             a[i] = val;
  2079.     }
  2080.  
  2081.     /**
  2082.      * Assigns the specified float value to each element of the specified array
  2083.      * of floats.
  2084.      *
  2085.      * @param a the array to be filled.
  2086.      * @param val the value to be stored in all elements of the array.
  2087.      */
  2088.     public static void fill(float[] a, float val) {
  2089.         fill(a, 0, a.length, val);
  2090.     }
  2091.  
  2092.     /**
  2093.      * Assigns the specified float value to each element of the specified 
  2094.      * range of the specified array of floats.
  2095.      *
  2096.      * @param a the array to be filled.
  2097.      * @param fromIndex the index of the first element (inclusive) to be
  2098.      *        filled with the specified value.
  2099.      * @param toIndex the index of the last element (exclusive) to be
  2100.      *        filled with the specified value.
  2101.      * @param val the value to be stored in all elements of the array.
  2102.      * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt>
  2103.      * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or
  2104.      *           <tt>toIndex > a.length</tt>
  2105.      */
  2106.     public static void fill(float[] a, int fromIndex, int toIndex, float val) {
  2107.         rangeCheck(a.length, fromIndex, toIndex);
  2108.         for (int i=fromIndex; i<toIndex; i++)
  2109.             a[i] = val;
  2110.     }
  2111.  
  2112.     /**
  2113.      * Assigns the specified Object reference to each element of the specified
  2114.      * array of Objects.
  2115.      *
  2116.      * @param a the array to be filled.
  2117.      * @param val the value to be stored in all elements of the array.
  2118.      */
  2119.     public static void fill(Object[] a, Object val) {
  2120.         fill(a, 0, a.length, val);
  2121.     }
  2122.  
  2123.     /**
  2124.      * Assigns the specified Object reference to each element of the specified 
  2125.      * range of the specified array of Objects.
  2126.      *
  2127.      * @param a the array to be filled.
  2128.      * @param fromIndex the index of the first element (inclusive) to be
  2129.      *        filled with the specified value.
  2130.      * @param toIndex the index of the last element (exclusive) to be
  2131.      *        filled with the specified value.
  2132.      * @param val the value to be stored in all elements of the array.
  2133.      * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt>
  2134.      * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or
  2135.      *           <tt>toIndex > a.length</tt>
  2136.      */
  2137.     public static void fill(Object[] a, int fromIndex, int toIndex,Object val){
  2138.         rangeCheck(a.length, fromIndex, toIndex);
  2139.         for (int i=fromIndex; i<toIndex; i++)
  2140.             a[i] = val;
  2141.     }
  2142.  
  2143.  
  2144.     // Misc
  2145.  
  2146.     /**
  2147.      * Returns a fixed-size list backed by the specified array.  (Changes to
  2148.      * the returned list "write through" to the array.)  This method acts
  2149.      * as bridge between array-based and collection-based APIs, in
  2150.      * combination with <tt>Collection.toArray</tt>.  The returned list is
  2151.      * serializable.
  2152.      *
  2153.      * @param a the array by which the list will be backed.
  2154.      * @return a list view of the specified array.
  2155.      * @see Collection#toArray()
  2156.      */
  2157.     public static List asList(Object[] a) {
  2158.     return new ArrayList(a);
  2159.     }
  2160.  
  2161.     private static class ArrayList extends AbstractList
  2162.                        implements java.io.Serializable
  2163.     {
  2164.     private Object[] a;
  2165.  
  2166.     ArrayList(Object[] array) {
  2167.         a = array;
  2168.     }
  2169.  
  2170.     public int size() {
  2171.         return a.length;
  2172.     }
  2173.  
  2174.     public Object[] toArray() {
  2175.         return (Object[]) a.clone();
  2176.     }
  2177.  
  2178.     public Object get(int index) {
  2179.         return a[index];
  2180.     }
  2181.  
  2182.     public Object set(int index, Object element) {
  2183.         Object oldValue = a[index];
  2184.         a[index] = element;
  2185.         return oldValue;
  2186.     }
  2187.     }
  2188. }
  2189.